﻿package smooth.core.object.googleMapLayerComponent {
	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.AntiAliasType;
	import flash.filters.*;
	
	public class GoogleMapLayerMenu extends Sprite {
		[Embed(source="Arial.ttf", fontFamily="myFont")] 
		public var myFont:Class;
		
		private var initialWidth:int;
		private var initialHeight:int;
		private var initialPosX:int;
		private var initialPosY:int;
		private var initialColor:int;
		private var closeButton:TextField;
		private var freezeButton:TextField;
		private var resizeButton:TextField;
		private var drawButton:TextField;
				
		public function GoogleMapLayerMenu(posX:int, posY:int, color:int) {
			// Init des attributs de l'objet
			this.initialPosX = posX;
			this.initialPosY = posY;
			this.initialColor = color;
			// Init des visuels du composant			
			initGraphics();
			// Init des events du composant
			initEvents();	
			// Init des process du composant
			initProcess();
		}
		
		private function initGraphics():void {
			var format:TextFormat= new TextFormat();
			format.font= "myFont";
			format.color= 0xFFFFFF;
			format.size = 15;
			
			this.closeButton = new TextField();
			this.closeButton.background = true;	
			this.closeButton.opaqueBackground = null;
			this.closeButton.backgroundColor = 0x000000;	
			this.closeButton.alpha = 0.5;
			this.closeButton.embedFonts = true;
			this.closeButton.selectable  = false;		 
			this.closeButton.text = "Close";
			this.closeButton.autoSize = TextFieldAutoSize.LEFT;
			this.closeButton.name = "Close";
			this.closeButton.width = 100;
			this.closeButton.height = 20;
			this.closeButton.x = this.initialPosX+10;
			this.closeButton.y = this.initialPosY+10;
			this.closeButton.setTextFormat(format);
			this.addChild(this.closeButton);	
			
			this.freezeButton = new TextField();
			this.freezeButton.background = true;	
			this.freezeButton.opaqueBackground = null;
			this.freezeButton.backgroundColor = 0x000000;	
			this.freezeButton.alpha = 0.5;
			this.freezeButton.embedFonts = true;
			this.freezeButton.selectable  = false;		 
			this.freezeButton.text = "Freeze";
			this.freezeButton.autoSize = TextFieldAutoSize.LEFT;
			this.freezeButton.name = "Freeze";
			this.freezeButton.width = 100;
			this.freezeButton.height = 20;
			this.freezeButton.x = this.initialPosX+10;
			this.freezeButton.y = this.initialPosY+40;
			this.freezeButton.setTextFormat(format);
			this.addChild(this.freezeButton);	
			
			this.resizeButton = new TextField();
			this.resizeButton.background = true;	
			this.resizeButton.opaqueBackground = null;
			this.resizeButton.backgroundColor = 0x000000;	
			this.resizeButton.alpha = 0.5;
			this.resizeButton.embedFonts = true;
			this.resizeButton.selectable  = false;		 
			this.resizeButton.text = "Resize";
			this.resizeButton.autoSize = TextFieldAutoSize.LEFT;
			this.resizeButton.name = "Resize";
			this.resizeButton.width = 100;
			this.resizeButton.height = 20;
			this.resizeButton.x = this.initialPosX+10;
			this.resizeButton.y = this.initialPosY+70;
			this.resizeButton.setTextFormat(format);
			this.addChild(this.resizeButton);
			
			this.drawButton = new TextField();
			this.drawButton.background = true;	
			this.drawButton.opaqueBackground = null;
			this.drawButton.backgroundColor = 0x000000;	
			this.drawButton.alpha = 0.5;
			this.drawButton.embedFonts = true;
			this.drawButton.selectable  = false;		 
			this.drawButton.text = "Draw";
			this.drawButton.autoSize = TextFieldAutoSize.LEFT;
			this.drawButton.name = "Draw";
			this.drawButton.width = 100;
			this.drawButton.height = 20;
			this.drawButton.x = this.initialPosX+10;
			this.drawButton.y = this.initialPosY+100;
			this.drawButton.setTextFormat(format);
			this.addChild(this.drawButton);	
			
		}
		
		private function initProcess():void {
		}
		
		private function initEvents():void {
			this.closeButton.addEventListener(TouchEvent.MOUSE_DOWN, this.doClose);
			this.freezeButton.addEventListener(TouchEvent.MOUSE_DOWN, this.doFreeze);
			this.resizeButton.addEventListener(TouchEvent.MOUSE_DOWN, this.doResize);
			this.drawButton.addEventListener(TouchEvent.MOUSE_DOWN, this.doDraw);
		}
		
		private function doClose(e:TouchEvent):void {
			this.parent.removeComponent();
			e.stopPropagation();
		}
		
		private function doFreeze(e:TouchEvent):void {
			// Désactive les autres actions
			if (this.parent.isResized()) {
				this.resizeButton.backgroundColor = 0x000000;
				this.parent.stopResize();
			}
			if (this.parent.isDrawing()) {
				this.drawButton.backgroundColor = 0x000000;
				this.parent.stopDraw();
			}
			// Traitement de l'action
			if (this.parent.isFreezed()) {
				this.freezeButton.backgroundColor = 0x000000;
				this.parent.stopFreeze();
			} else {
				this.freezeButton.backgroundColor = this.parent.getColor();
				this.parent.freeze();
			}
			
		}
		
		private function doResize(e:TouchEvent):void {
			// Désactive les autres actions
			if (this.parent.isFreezed()) {
				this.freezeButton.backgroundColor = 0x000000;
				this.parent.stopFreeze();
			}
			if (this.parent.isDrawing()) {
				this.drawButton.backgroundColor = 0x000000;
				this.parent.stopDraw();
			}
			// Traitement de l'action
			if (this.parent.isResized()) {
				this.resizeButton.backgroundColor = 0x000000;
				this.parent.stopResize();
			} else {
				this.resizeButton.backgroundColor = this.parent.getColor();
				this.parent.resize();
			}
			
		}
		
		private function doDraw(e:TouchEvent):void {
			// Désactive les autres actions
			if (this.parent.isResized()) {
				this.resizeButton.backgroundColor = 0x000000;
				this.parent.stopResize();
			}
			// Traitement de l'action
			if (this.parent.isDrawing()) {
				this.drawButton.backgroundColor = 0x000000;
				this.parent.stopDraw();
			} else {
				if (!this.parent.isFreezed()) {
					this.freezeButton.backgroundColor = this.parent.getColor();
					this.parent.freeze();
				}
				this.drawButton.backgroundColor = this.parent.getColor();
				this.parent.draw();
			}
			
		}
		
	}
	
}